package com.cyl.manager.aws.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cyl.manager.aws.convert.ProfitDetailsStatisticsConvert;
import com.cyl.manager.aws.domain.entity.ProfitDetailsStatistics;
import com.cyl.manager.aws.pojo.query.ProfitDetailsStatisticsQuery;
import com.cyl.manager.aws.pojo.vo.ProfitDetailsStatisticsSumVO;
import com.cyl.manager.aws.pojo.vo.ProfitDetailsStatisticsVO;
import com.cyl.manager.aws.service.ProfitDetailsStatisticsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.redis.RedisService;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 毛利润统计明细Controller
 *
 * @author hxw
 * @date 2025-04-14
 */
@Api(description ="毛利润统计明细接口列表")
@RestController
@RequestMapping("/aws/profitDetailsStatistics")
public class ProfitDetailsStatisticsController extends BaseController {
    @Autowired
    private ProfitDetailsStatisticsService service;
    @Autowired
    private RedisService redisService;
    

    
    @ApiOperation("查询毛利润统计明细列表")
    @PreAuthorize("@ss.hasPermi('aws:profitDetailsStatistics:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<Object>> list(@RequestBody ProfitDetailsStatisticsQuery query, Pageable page) {
        Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
         	query.setDeptZbjId(qrDeptZbjId);
        }
    	List<ProfitDetailsStatistics> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(service.dos2vos(list, query.getType()), page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出毛利润统计明细列表")
    @PreAuthorize("@ss.hasPermi('aws:profitDetailsStatistics:export')")
    @Log(title = "毛利润统计明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(ProfitDetailsStatisticsQuery query) {
        Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
         	query.setDeptZbjId(qrDeptZbjId);
        }
        List<ProfitDetailsStatistics> list = service.selectList(query, null);
        String type = query.getType();
        if(type!=null && type.equals("sum")) {
        	ExcelUtil<ProfitDetailsStatisticsSumVO> utilsum = new ExcelUtil<>(ProfitDetailsStatisticsSumVO.class);
   	        return ResponseEntity.ok(utilsum.writeExcel(service.dos2vosVoSum(list), "利润统计"));
		}else {
	        ExcelUtil<ProfitDetailsStatisticsVO> util = new ExcelUtil<>(ProfitDetailsStatisticsVO.class);
	        return ResponseEntity.ok(util.writeExcel(service.dos2vosVo(list), "利润统计"));
		}
    }
    
    
    @ApiOperation("获取毛利润统计明细详细信息")
    @PreAuthorize("@ss.hasPermi('aws:profitDetailsStatistics:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<ProfitDetailsStatistics> getInfo(@PathVariable("id") String id) {
        return ResponseEntity.ok(service.selectById(id));
    } 
    
    /**
     * 	获取最新更新时间
     * @return
     */
    @ApiOperation("获取最新更新时间")
    @PreAuthorize("@ss.hasPermi('aws:profitDetailsStatistics:query')")
    @GetMapping(value = "/selectMaxCreateTime")
    public ResponseEntity<String> selectMaxCreateTime() {
        return ResponseEntity.ok(service.selectMaxCreateTime());
    }
    @ApiOperation("获取最新数据统计")
    @GetMapping(value = "/insertProfitDate")
    @PreAuthorize("@ss.hasPermi('aws:profitDetailsStatistics:insertProfitDate')")
    public ResponseEntity<String> insertProfitDate(String date) {
    	String ok = "操作成功！";
    	LocalDate thisLocalDate = LocalDate.now();
    	if(date!=null) {
    		thisLocalDate = LocalDate.parse(date);
    	}
        LocalDateTime strTime = LocalDateTime.of(thisLocalDate, LocalTime.MIN);
    	LocalDateTime endTime = LocalDateTime.of(thisLocalDate, LocalTime.MAX);
    	String redisKey = "admin_profit_shop_123456";
        String redisValue = redisKey+"_"+strTime.toString();
        	try{    
    		    redisService.lock(redisKey,redisValue,60*30);
    		    int a = service.insertProfitDay(strTime, endTime);
    		    ok = ok + "产生"+a+"条数据 ";
        	}catch (Exception e){
        		 ok = "1: "+e.getMessage();
        	}finally {
    	        try {
    	        	redisService.unLock(redisKey,redisValue);
	            }catch (Exception e){
	       		    ok = "2: "+e.getMessage();
	            }
        	}
		return ResponseEntity.ok(ok);
    }
    

    
}
